package com.yilin.tms.message.common.interceptor;


import org.jetbrains.annotations.NotNull;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/***
 * 即时通讯握手拦截器
 * */
public class ChatHandshakeInterceptor extends HttpSessionHandshakeInterceptor {

    // 握手前
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, @NotNull Map<String, Object> attributes) throws Exception {
        if(request != null){
            HttpServletRequest servletRequest=((ServletServerHttpRequest) request).getServletRequest();
            String userId = servletRequest.getParameter("userId");
            String token = servletRequest.getHeader("token");
            attributes.put("HTTP.SESSION.userId",userId);
            attributes.put("HTTP.SESSION.token",token);
        }
        return super.beforeHandshake(request, response, wsHandler, attributes);
    }

    // 握手后
    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) {
        super.afterHandshake(request, response, wsHandler, ex);
    }
}
